package com.changgou.business.listener;

import okhttp3.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author: HuangFu
 * @Date: 2020/1/31 17:30
 * @Description: 广告数据变更监听
 */
@Component
public class AdListener {

    @Value("${system.nginx.ad_update_url}")
    private String ad_update_url;

    @RabbitListener(queues = "ad_update_queue")
    public void consumeMessage(String message){
        //设置请求地址
        String url = ad_update_url + "?position="+message;
        //创建Http请求客户端
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建请求
        Request request = new Request.Builder().url(url).build();
        //发送请求
        Call call = okHttpClient.newCall(request);
        //获取响应结果
        call.enqueue(new Callback() {
            /**
             * 失败回调
             * @param call
             * @param e
             */
            @Override
            public void onFailure(Call call, IOException e) {
               e.printStackTrace();
                System.out.println("广告数据更新失败");
            }

            /**
             * 成功回调
             * @param call
             * @param response
             * @throws IOException
             */
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println("广告数据更新成功:"+response.message());
            }
        });
    }


}
